package com.kye.express.business.jpush;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatDialogFragment;
import com.kye.express.business.login.LoginActivity;
import com.kye.express.utils.StringUtils;
import com.kye.model.UserSession;
import com.kye.model.bean.jpush.PushFeedbackModel;
import com.kye.model.bean.jpush.PushNewsModel;
import com.kye.model.bean.jpush.PushPlaceOrderModel;

/**
 * push消息dialog展示
 */
public class JpushDialogFragment extends BaseAppCompatDialogFragment {

    public static JpushDialogFragment newOtherLoginDialog(PushPlaceOrderModel pushPlaceOrderModel) {
        JpushDialogFragment fragment = new JpushDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(JpushDialogArgKeyUtils.ARG_DATA, pushPlaceOrderModel);
        bundle.putSerializable(JpushDialogArgKeyUtils.ARG_ACTION_ID, JpushDialogArgKeyUtils.PushDialogActionId.otherLogin);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static JpushDialogFragment newNoticeDialog(PushNewsModel pushNewsModel) {
        JpushDialogFragment fragment = new JpushDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(JpushDialogArgKeyUtils.ARG_DATA, pushNewsModel);
        bundle.putSerializable(JpushDialogArgKeyUtils.ARG_ACTION_ID, JpushDialogArgKeyUtils.PushDialogActionId.notice);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static JpushDialogFragment newFeedbackDialog(PushFeedbackModel pushFeedbackModel) {
        JpushDialogFragment fragment = new JpushDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(JpushDialogArgKeyUtils.ARG_DATA, pushFeedbackModel);
        bundle.putSerializable(JpushDialogArgKeyUtils.ARG_ACTION_ID, JpushDialogArgKeyUtils.PushDialogActionId.feedback);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle == null) {
            getActivity().finish();
        }
        MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity());
        JpushDialogArgKeyUtils.PushDialogActionId actionId = (JpushDialogArgKeyUtils.PushDialogActionId) bundle.getSerializable(JpushDialogArgKeyUtils.ARG_ACTION_ID);

        if (actionId == JpushDialogArgKeyUtils.PushDialogActionId.otherLogin) {
            buildOtherLoginDialog(builder);
        } else if (actionId == JpushDialogArgKeyUtils.PushDialogActionId.notice) {
            PushNewsModel pushNewsModel = (PushNewsModel) bundle.getSerializable(JpushDialogArgKeyUtils.ARG_DATA);
            buildNoticeDialog(builder, pushNewsModel);
        } else if (actionId == JpushDialogArgKeyUtils.PushDialogActionId.feedback) {
            PushFeedbackModel pushFeedbackModel = (PushFeedbackModel) bundle.getSerializable(JpushDialogArgKeyUtils.ARG_DATA);
            buildFeedbackDialog(builder, pushFeedbackModel);
        }

        return builder.build();
    }

    private void buildOtherLoginDialog(MaterialDialog.Builder builder) {
        builder.title(R.string.jpush_other_login_title)
                .content(R.string.jpush_other_login_message)
                .positiveText(R.string.common_know)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        UserSession.getInstance().setUser(null);
                        LoginActivity.startActivity(getActivity());
                        dismiss();
                        getActivity().finish();
                    }
                });
    }

    private void buildNoticeDialog(MaterialDialog.Builder builder, PushNewsModel pushNewsModel) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_jpush_notice, null);
        TextView contentView = view.findViewById(R.id.tv_jpush_notice_content);
        TextView timeView = view.findViewById(R.id.tv_jpush_notice_time);
        Button detailButton = view.findViewById(R.id.btn_jpush_notice_details);
        if (pushNewsModel != null) {
            if (!StringUtils.isNullOrEmpty(pushNewsModel.getContInfo())) {
                contentView.setText(pushNewsModel.getContInfo());
            } else {
                contentView.setText(pushNewsModel.getTitle());
            }
            timeView.setText(pushNewsModel.getMsgSendTime());
            detailButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //TODO 跳转到通知详情页面

                    dismiss();
                    getActivity().finish();
                }
            });
        }
        builder.customView(view, false);
    }

    private void buildFeedbackDialog(MaterialDialog.Builder builder, PushFeedbackModel pushFeedbackModel) {
        if (pushFeedbackModel != null) {
            builder.title(R.string.common_reminder)
                    .content(pushFeedbackModel.getContInfo())
                    .positiveText(R.string.common_know)
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dismiss();
                            getActivity().finish();
                        }
                    });
        }
    }
}
